package com.security.lambda.function;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 通过构造函数创建对象
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2017-11-09 15:19
 **/
public class TriMain {

    public static void main(String[] args) {

        // lambda 表达式
        TriFunction<String, Integer, Integer, TriEntity> triEntityTriFunction = (String name, Integer hand, Integer foot) -> new TriEntity(name, hand, foot);

        List<TriEntity> triEntityList = new ArrayList<>();
        triEntityList.add(triEntityTriFunction.apply("猫", 2, 2));
        triEntityList.add(triEntityTriFunction.apply("狗", 2, 2));
        triEntityList.add(triEntityTriFunction.apply("猪", 0, 4));

        triEntityList.stream().forEach(triEntity -> System.out.println(triEntity));

        // 自定义Function函数式接口 构造函数引用
        TriFunction<String, Integer, Integer, TriEntity> entityTriFunction = TriEntity::new;

        triEntityList.add(entityTriFunction.apply("蛇", 0, 0));
        triEntityList.add(entityTriFunction.apply("鸡", 0, 2));
        triEntityList.add(entityTriFunction.apply("猪", 1, 2));

        triEntityList.stream().forEach(triEntity -> System.out.println(triEntity));


        // 使用lambda排序-1
        triEntityList.sort((TriEntity t1, TriEntity t2) -> t1.getFoot().compareTo(t1.getFoot()));
        // 使用lambda排序-2 ,lambda 根据上下文来推断Lambda表达式参数的类型
        triEntityList.sort((t1, t2) -> t1.getFoot().compareTo(t1.getFoot()));
        // 使用lambda排序-3 通过Comparator.comparing静态辅助方法
        triEntityList.sort(Comparator.comparing(triEntity -> triEntity.getFoot()));
        // 使用 方法引用 排序(正序)
        triEntityList.sort(Comparator.comparing(TriEntity::getName));
        // 使用 方法引用 排序(倒序)  -- reversed() 倒序
        triEntityList.sort(Comparator.comparing(TriEntity::getName).reversed());
        // 使用 方法引用 排序(倒序) name一样,按照hand排序
        triEntityList.sort(Comparator.comparing(TriEntity::getName).reversed().thenComparing(TriEntity::getHand));

        System.out.println();
        triEntityList.stream().forEach(triEntity -> System.out.println(triEntity));
    }

}
